package com.example;

import com.example.entity.Product;
import com.example.entity.User;
import com.example.service.ProductService;
import com.example.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * 数据初始化测试类
 * 手动执行此测试来初始化数据
 */
@Slf4j
@SpringBootTest
public class DataInitializerTest {

    @Autowired
    private UserService userService;

    @Autowired
    private ProductService productService;

    /**
     * 初始化所有测试数据
     * 运行此测试方法来创建表并初始化数据
     */
    @Test
    public void initAllData() {
        log.info("========================================");
        log.info("开始初始化测试数据");
        log.info("========================================");

        // 初始化用户数据
        initUsers();

        // 初始化商品数据
        initProducts();

        log.info("========================================");
        log.info("测试数据初始化完成");
        log.info("========================================");
    }

    /**
     * 仅初始化用户数据
     */
    @Test
    public void initUsers() {

        User user1 = new User();
        user1.setUsername("admin6");
        user1.setPassword("123456");
        user1.setAge(30);
        user1.setNum(666);
        user1.setStr1("aa");

        userService.save(user1);

        log.info("initUsers finish");
    }

    /**
     * 仅初始化商品数据
     */
    @Test
    public void initProducts() {
        Product product1 = new Product();
        product1.setProductName("iPhone 15 Pro");
        product1.setProductCode("IPHONE15PRO");
        product1.setCategory("手机");
        product1.setPrice(new BigDecimal("7999.00"));
        product1.setStock(100);
        product1.setDescription("Apple iPhone 15 Pro，全新A17 Pro芯片");
        product1.setStatus(1);
        product1.setCreateTime(LocalDateTime.now());
        product1.setUpdateTime(LocalDateTime.now());
        productService.save(product1);

        Product product2 = new Product();
        product2.setProductName("MacBook Pro 14");
        product2.setProductCode("MACBOOKPRO14");
        product2.setCategory("电脑");
        product2.setPrice(new BigDecimal("14999.00"));
        product2.setStock(50);
        product2.setDescription("Apple MacBook Pro 14英寸，M3芯片");
        product2.setStatus(1);
        product2.setCreateTime(LocalDateTime.now());
        product2.setUpdateTime(LocalDateTime.now());
        productService.save(product2);

        Product product3 = new Product();
        product3.setProductName("AirPods Pro");
        product3.setProductCode("AIRPODSPRO");
        product3.setCategory("耳机");
        product3.setPrice(new BigDecimal("1999.00"));
        product3.setStock(200);
        product3.setDescription("Apple AirPods Pro，主动降噪");
        product3.setStatus(1);
        product3.setCreateTime(LocalDateTime.now());
        product3.setUpdateTime(LocalDateTime.now());
        productService.save(product3);

        log.info("initProducts finish");
    }

    /**
     * 清空所有数据
     */
    @Test
    public void clearAllData() {
        log.warn("========================================");
        log.warn("开始清空所有数据");
        log.warn("========================================");

        long userCount = userService.count();
        long productCount = productService.count();

        if (userCount > 0) {
            userService.remove(null);
            log.warn("已清空 {} 条用户数据", userCount);
        }

        if (productCount > 0) {
            productService.remove(null);
            log.warn("已清空 {} 条商品数据", productCount);
        }

        log.warn("========================================");
        log.warn("数据清空完成");
        log.warn("========================================");
    }
}
